package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillCodeMsg;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("path")
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("getPath")
    public Result getPath(String uuid, String verifyCode) {
        if (verifyCode.isEmpty()) {
            throw new BusinessException(SeckillCodeMsg.VERIFY_CODE_NOT_EMPTY);
        }
        String code = redisService.get(SeckillKeyPrefix.SECKILL_VERIFY_CODE, uuid, String.class);
        if (!code.equals(verifyCode)) {
            throw new BusinessException(SeckillCodeMsg.VERIFY_CODE_ERROR);
        }
        //生成path
        String path = UUID.randomUUID().toString().replace("-", "");
        //存入redis
        redisService.set(SeckillKeyPrefix.SECKILL_PATH, uuid, path);
        return new Result().success(path);
    }
}
